package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class TourDAO implements TourInterface {
	private ResultSet rs = null;
	private Connection conn;
	private Statement stmt;
	List<Tour> dstour = new ArrayList<Tour>();

	public TourDAO() {
		try {
			conn = Connection_JDBC.getConnection();
			stmt = (Statement) conn.createStatement(
					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();
		}
	}
	
	public boolean InsertDatTour(String name,String email,String number,String idCTT,String phone){
		try {
			String sql="INSERT INTO `booktour`.`dattour` (`Ten`, `Email`, `SoLuong`, `MaCT`, `SDT`) VALUES ('"+name+"', '"+email+"', '"+number+"', '"+idCTT+"', '"+phone+"')";
			stmt.execute(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	public List<ChiTietTour> getChiTietTour(String MaTour) {
		List<ChiTietTour> dsCTT= new ArrayList<>();
		try {
			rs = stmt.executeQuery("SELECT * FROM cttour WHERE MaTour='"
					+ MaTour + "'");
			while (rs.next()) {
				ChiTietTour ct= new ChiTietTour();
				ct.setMaTour(rs.getInt("MaTour"));
				ct.setMaCT(rs.getInt("MaCT"));
				ct.setNgayDi(rs.getDate("NgayDi"));
				dsCTT.add(ct);
			}
			rs.close();
			return dsCTT;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public Tour getTourByID(String ID) {
		Tour t = new Tour();
		try {
			rs = stmt.executeQuery("SELECT * FROM tour WHERE MaTour='" + ID
					+ "'");
			rs.first();
			t.setMatour(rs.getInt("MaTour"));
			t.setTentour(rs.getString("TenTour"));
			t.setThoigian(rs.getString("ThoiGian"));
			t.setDiemkhoihanh(rs.getString("DiemKhoiHanh"));
			t.setNoidung(rs.getString("NoiDung"));
			t.setBaogom(rs.getString("BaoGom"));
			t.setKhongbaogom(rs.getString("KhongBaoGom"));
			t.setGiachotreem(rs.getString("GiaChoTreEm"));
			t.setGhichu(rs.getString("GhiChu"));
			t.setGia(rs.getString("Gia"));
			t.setImage(rs.getString("Image"));
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}

	@Override
	public List<Tour> DanhSachTour() {
		try {
			rs = stmt.executeQuery("SELECT * FROM tour");
			while (rs.next()) {
				Tour t = new Tour();
				t.setMatour(rs.getInt("MaTour"));
				t.setTentour(rs.getString("TenTour"));
				t.setThoigian(rs.getString("ThoiGian"));
				t.setDiemkhoihanh(rs.getString("DiemKhoiHanh"));
				t.setNoidung(rs.getString("NoiDung"));
				t.setBaogom(rs.getString("BaoGom"));
				t.setKhongbaogom(rs.getString("KhongBaoGom"));
				t.setGiachotreem(rs.getString("GiaChoTreEm"));
				t.setGhichu(rs.getString("GhiChu"));
				t.setGia(rs.getString("Gia"));
				t.setImage(rs.getString("Image"));
				dstour.add(t);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dstour;
	}
}
